######################
# Appendix: DENMARK  #    
# Figure 7 & 11      #
######################

###load library
library(openxlsx)
library(psych)
library(ggplot2)
library(gridExtra)
library(dtw)
library(tseries)
library(TSclust)
library(tidyverse)
library(zoo)
source("extract.R")


#######----------Figure 7: All patterns of indices (Denmark)--------##########


#######################Laakso Taagepera ENPP###########################

denmark_final<-read.csv("DENMARK_FINAL.csv")
attach(denmark_final)
denmark_final$indep_dnk=1-(denmark_final$allp_dnk/100)
denmark_final$socd<-mean.socd/100

#calculating macropartisanship: basic formulation 
denmark_final$macropartisanship_1<-(denmark_final$socd/(denmark_final$allp_dnk/100))*(effpar_ele/2)*(((denmark_final$allp_dnk/100)/denmark_final$indep_dnk)*(1/effpar_ele^2))
describe(denmark_final$macropartisanship_1)
denmark_final$perSOCD<-denmark_final$socd/(denmark_final$allp_dnk/100)

#weighting process: final fomulation
denmark_final$macropartisanship_1<-ifelse((is.infinite(denmark_final$macropartisanship_1) & (denmark_final$macropartisanship_1> 0)) , 0.01, denmark_final$macropartisanship_1)
denmark_final$macrop_var<-var(denmark_final$macropartisanship_1,na.rm=T)
denmark_final$macroper_var<-var(denmark_final$socd,na.rm=T)

denmark_final$macropartisanship_DNK_LT<-(denmark_final$macropartisanship_1/denmark_final$macrop_var+denmark_final$perSOCD/denmark_final$macroper_var)/
  (1/denmark_final$macrop_var+1/denmark_final$macroper_var)


###-------without nested s2


#calculating macropartisanship: basic formulation 
denmark_final$macropartisanship_1<-(denmark_final$socd/(denmark_final$allp_dnk/100))*
  (effpar_ele/2)*(((denmark_final$allp_dnk/100)/denmark_final$indep_dnk))
describe(denmark_final$macropartisanship_1)
denmark_final$perSOCD<-denmark_final$socd/(denmark_final$allp_dnk/100)

#weighting process: final fomulation
denmark_final$macropartisanship_1<-ifelse((is.infinite(denmark_final$macropartisanship_1) & (denmark_final$macropartisanship_1> 0)) , 0.01, denmark_final$macropartisanship_1)
denmark_final$macrop_var<-var(denmark_final$macropartisanship_1,na.rm=T)
denmark_final$macroper_var<-var(denmark_final$socd,na.rm=T)

denmark_final$macropartisanship_DNK_LT_withoutnested<-(denmark_final$macropartisanship_1/denmark_final$macrop_var+denmark_final$perSOCD/denmark_final$macroper_var)/
  (1/denmark_final$macrop_var+1/denmark_final$macroper_var)

###-----simply product the proportion of independent 


#calculating macropartisanship: basic formulation 
denmark_final$macropartisanship_1<-(denmark_final$socd/(denmark_final$allp_dnk/100))*
  (effpar_ele/2)*(denmark_final$indep_dnk)
describe(denmark_final$macropartisanship_1)
denmark_final$perSOCD<-denmark_final$socd/(denmark_final$allp_dnk/100)

#weighting process: final fomulation
denmark_final$macropartisanship_1<-ifelse((is.infinite(denmark_final$macropartisanship_1) & (denmark_final$macropartisanship_1> 0)) , 0.01, denmark_final$macropartisanship_1)
denmark_final$macrop_var<-var(denmark_final$macropartisanship_1,na.rm=T)
denmark_final$macroper_var<-var(denmark_final$socd,na.rm=T)

denmark_final$macropartisanship_DNK_LT_prindependent<-(denmark_final$macropartisanship_1/denmark_final$macrop_var+denmark_final$perSOCD/denmark_final$macroper_var)/
  (1/denmark_final$macrop_var+1/denmark_final$macroper_var)





###########################Molinar############################

###-----The simple Molinar ENPP
#calculating macropartisanship: basic formulation 
denmark_final$macropartisanship_1<-(denmark_final$socd/(denmark_final$allp_dnk/100))*(enpp_np/2)*(((denmark_final$allp_dnk/100)/denmark_final$indep_dnk)*(1/enpp_np^2))
describe(denmark_final$macropartisanship_1)
denmark_final$perSOCD<-denmark_final$socd/(denmark_final$allp_dnk/100)

#weighting process: final fomulation
denmark_final$macropartisanship_1<-ifelse((is.infinite(denmark_final$macropartisanship_1) & (denmark_final$macropartisanship_1> 0)) , 0.01, denmark_final$macropartisanship_1)
denmark_final$macrop_var<-var(denmark_final$macropartisanship_1,na.rm=T)
denmark_final$macroper_var<-var(denmark_final$socd,na.rm=T)

denmark_final$macropartisanship_DNK_NP<-(denmark_final$macropartisanship_1/denmark_final$macrop_var+denmark_final$perSOCD/denmark_final$macroper_var)/
  (1/denmark_final$macrop_var+1/denmark_final$macroper_var)


###-------without nested s2

#calculating macropartisanship: basic formulation 
denmark_final$macropartisanship_1<-(denmark_final$socd/(denmark_final$allp_dnk/100))*(enpp_np/2)*(((denmark_final$allp_dnk/100)/denmark_final$indep_dnk))
describe(denmark_final$macropartisanship_1)
denmark_final$perSOCD<-denmark_final$socd/(denmark_final$allp_dnk/100)

#weighting process: final fomulation
denmark_final$macropartisanship_1<-ifelse((is.infinite(denmark_final$macropartisanship_1) & (denmark_final$macropartisanship_1> 0)) , 0.01, denmark_final$macropartisanship_1)
denmark_final$macrop_var<-var(denmark_final$macropartisanship_1,na.rm=T)
denmark_final$macroper_var<-var(denmark_final$socd,na.rm=T)

denmark_final$macropartisanship_DNK_NP_withoutnested<-(denmark_final$macropartisanship_1/denmark_final$macrop_var+denmark_final$perSOCD/denmark_final$macroper_var)/
  (1/denmark_final$macrop_var+1/denmark_final$macroper_var)


###-----simply product the proportion of independent 

#calculating macropartisanship: basic formulation 
denmark_final$macropartisanship_1<-(denmark_final$socd/(denmark_final$allp_dnk/100))*(enpp_np/2)*(denmark_final$indep_dnk)
describe(denmark_final$macropartisanship_1)
denmark_final$perSOCD<-denmark_final$socd/(denmark_final$allp_dnk/100)

#weighting process: final fomulation
denmark_final$macropartisanship_1<-ifelse((is.infinite(denmark_final$macropartisanship_1) & (denmark_final$macropartisanship_1> 0)) , 0.01, denmark_final$macropartisanship_1)
denmark_final$macrop_var<-var(denmark_final$macropartisanship_1,na.rm=T)
denmark_final$macroper_var<-var(denmark_final$socd,na.rm=T)

denmark_final$macropartisanship_DNK_NP_prindependent<-(denmark_final$macropartisanship_1/denmark_final$macrop_var+denmark_final$perSOCD/denmark_final$macroper_var)/
  (1/denmark_final$macrop_var+1/denmark_final$macroper_var)



###constituting data frame for comparison

df_mp_enpp<-data.frame(denmark_final$socd,denmark_final$yearMon,
                       denmark_final$macropartisanship_DNK_LT,
                       denmark_final$macropartisanship_DNK_LT_withoutnested,
                       denmark_final$macropartisanship_DNK_LT_prindependent,
                       denmark_final$macropartisanship_DNK_NP,
                       denmark_final$macropartisanship_DNK_NP_withoutnested,
                       denmark_final$macropartisanship_DNK_NP_prindependent)

##################mp with DRA##############################


#load data
de<-read.csv("denmark_extract_withindep.csv")
attach(de)
describe(de)
de<-na.omit(de)

#setting time-series information
varname<-de$VARNAME
date<-as.Date(YearMon)
index<-(de$value)/100
ncases<-NULL

#using "extract" to compute macropartisanship
output_denmark<-extract(varname,date=date,index,ncases,begindt=ISOdate(1960,1,1),
                        enddt = ISOdate(2019,6,1), unit="M",npass=2)
display(output_denmark)
summary(output_denmark)


df_ld_denmark<-data.frame(output_denmark$varname, output_denmark$loadings1,output_denmark$loadings2)
colnames(df_ld_denmark)<-c("Party names","Loadings1","Loadings2")
print(xtable(df_ld_denmark), include.rownames=FALSE)


#factor scores of 2 dimensions
mpartisan1_denmark_DRA<-output_denmark$latent1
mpartisan2_denmark<-output_denmark$latent2

yearMon<-as.Date(sprintf("%.2f.01", output_denmark$period), format = "%Y.%m.%d")

###constituting data frame for comparison
df_dra_denmark<-data.frame(as.factor(yearMon),mpartisan1_denmark_DRA)
colnames(df_dra_denmark)<-c("denmark_final.yearMon","mpartisan1_denmark_DRA")

#join two data.frame
tb_denmark_mpcomparison<-inner_join(df_dra_denmark,df_mp_enpp,by=c("denmark_final.yearMon"))
write.csv(tb_denmark_mpcomparison, "tb_denmark_mpcomparison.csv")

##################plot all indices#####################
#plot the simple macropartisanship and the modified

denmark_fig2<-tb_denmark_mpcomparison 
colnames(denmark_fig2)<-c( "denmark_final.yearMon","DRA","SOCD","LT-nested","LT-simple","LT-indep","NP-nested","NT-simple","NP-independent")
tidy_all_indices<-denmark_fig2 %>% 
  gather(key="variable", value="value", -denmark_final.yearMon)

windows(10,10)
denmark_compare<-ggplot(tidy_all_indices, aes(as.yearmon(x = denmark_final.yearMon), y = value)) + 
  geom_line(aes(color = variable), size = 1) +
  theme_minimal()
ggsave(plot=denmark_compare,filename="denmark_compare.pdf",width=12,height=8)




#######---------Figure 9: Dynamic time warping distance for the Denmark case----------########

##################DTW############################

windows(30,25)
pdf("fig_dtw_denmark.pdf")
split.screen(c(3,3))

###----displaying dynamic time warping:

###LT indep
#simply calulating dtw distance
df_dtw<-data.frame(tb_denmark_mpcomparison$denmark_final.socd ,tb_denmark_mpcomparison$denmark_final.macropartisanship_DNK_LT_prindependent)
df_dtw<-na.omit(df_dtw)

ts_a<-df_dtw$tb_denmark_mpcomparison.denmark_final.socd
ts_b<-df_dtw$tb_denmark_mpcomparison.denmark_final.macropartisanship_DNK_LT_prindependent

d2 <- dtw::dtw(ts_a, ts_b,step.pattern = symmetric1)
d2$distance

screen(1)
dtwPlotTwoWay(d2, ts_a, ts_b,main="CONVENTIONAL vs LT-indep", cex.main = 0.6)


###NP indep
#simply calulating dtw distance
df_dtw<-data.frame(tb_denmark_mpcomparison$denmark_final.socd ,tb_denmark_mpcomparison$denmark_final.macropartisanship_DNK_NP_prindependent)
df_dtw<-na.omit(df_dtw)

ts_a<-df_dtw$tb_denmark_mpcomparison.denmark_final.socd
ts_b<-df_dtw$tb_denmark_mpcomparison.denmark_final.macropartisanship_DNK_NP_prindependent

d2 <- dtw::dtw(ts_a, ts_b,step.pattern = symmetric1)
d2$distance


screen(2)
dtwPlotTwoWay(d2, ts_a, ts_b,main="CONVENTIONAL vs NP-indep", cex.main = 0.6)


###LT simple
#simply calulating dtw distance
df_dtw<-data.frame(tb_denmark_mpcomparison$denmark_final.socd ,tb_denmark_mpcomparison$denmark_final.macropartisanship_DNK_LT_withoutnested)
df_dtw<-na.omit(df_dtw)

ts_a<-df_dtw$tb_denmark_mpcomparison.denmark_final.socd
ts_b<-df_dtw$tb_denmark_mpcomparison.denmark_final.macropartisanship_DNK_LT_withoutnested

d2 <- dtw::dtw(ts_a, ts_b,step.pattern = symmetric1)
d2$distance



screen(3)
dtwPlotTwoWay(d2, ts_a, ts_b,main="CONVENTIONAL vs LT-simple", cex.main = 0.6)


###NP simple
#simply calulating dtw distance
df_dtw<-data.frame(tb_denmark_mpcomparison$denmark_final.socd ,tb_denmark_mpcomparison$denmark_final.macropartisanship_DNK_NP_withoutnested)
df_dtw<-na.omit(df_dtw)

ts_a<-df_dtw$tb_denmark_mpcomparison.denmark_final.socd
ts_b<-df_dtw$tb_denmark_mpcomparison.denmark_final.macropartisanship_DNK_NP_withoutnested

d2 <- dtw::dtw(ts_a, ts_b,step.pattern = symmetric1)
d2$distance


screen(4)

dtwPlotTwoWay(d2, ts_a, ts_b,main="CONVENTIONAL vs NP-simple", cex.main = 0.6)


###LT nested
#simply calulating dtw distance
df_dtw<-data.frame(tb_denmark_mpcomparison$denmark_final.socd ,tb_denmark_mpcomparison$denmark_final.macropartisanship_DNK_LT)
df_dtw<-na.omit(df_dtw)

ts_a<-df_dtw$tb_denmark_mpcomparison.denmark_final.socd
ts_b<-df_dtw$tb_denmark_mpcomparison.denmark_final.macropartisanship_DNK_LT

d2 <- dtw::dtw(ts_a, ts_b,step.pattern = symmetric1)
d2$distance

screen(5)

dtwPlotTwoWay(d2, ts_a, ts_b,main="CONVENTIONAL vs LT-nested", cex.main = 0.6)


###NP nested
#simply calulating dtw distance
df_dtw<-data.frame(tb_denmark_mpcomparison$denmark_final.socd ,tb_denmark_mpcomparison$denmark_final.macropartisanship_DNK_NP)
df_dtw<-na.omit(df_dtw)

ts_a<-df_dtw$tb_denmark_mpcomparison.denmark_final.socd
ts_b<-df_dtw$tb_denmark_mpcomparison.denmark_final.macropartisanship_DNK_NP

d2 <- dtw::dtw(ts_a, ts_b,step.pattern = symmetric1)
d2$distance


screen(6)

dtwPlotTwoWay(d2, ts_a, ts_b,main="CONVENTIONAL vs NP-nested", cex.main = 0.6)




####DRA
#simply calulating dtw distance
df_dtw<-data.frame(tb_denmark_mpcomparison$denmark_final.socd ,tb_denmark_mpcomparison$mpartisan1_denmark_DRA)
df_dtw<-na.omit(df_dtw)

ts_a<-df_dtw$tb_denmark_mpcomparison.denmark_final.socd
ts_b<-df_dtw$tb_denmark_mpcomparison.mpartisan1_denmark_DRA

d2 <- dtw::dtw(ts_a, ts_b,step.pattern = symmetric1)
d2$distance

screen(7)


dtwPlotTwoWay(d2, ts_a, ts_b,main="CONVENTIONAL vs DRA", cex.main = 0.6)


dev.off()


